import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useUserStore } from './user'
import { getUnreadCount, getMessages, markAsRead } from '@/api/message'

export const useMessageStore = defineStore('message', () => {
  const unreadCount = ref(0)
  const messages = ref([])
  const userStore = useUserStore()

  // 获取未读消息数量
  async function getUnreadCountAction() {
    try {
      const response = await getUnreadCount()
      unreadCount.value = response.data
      return Promise.resolve(response.data)
    } catch (error) {
      return Promise.reject(error)
    }
  }

  // 获取消息列表
  async function getMessagesAction() {
    try {
      const response = await getMessages()
      messages.value = response.data
      return Promise.resolve(response.data)
    } catch (error) {
      return Promise.reject(error)
    }
  }

  // 标记消息为已读
  async function markAsReadAction(messageId) {
    try {
      await markAsRead(messageId)
      await getUnreadCountAction()
      return Promise.resolve()
    } catch (error) {
      return Promise.reject(error)
    }
  }

  return {
    unreadCount,
    messages,
    getUnreadCountAction,
    getMessagesAction,
    markAsReadAction
  }
}) 